package io.renren.modules.contract.dao;

import io.renren.modules.contract.entity.*;
import io.renren.modules.sys.dao.BaseDao;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 收款合同表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-07-31 16:08:55
 */
@Mapper
public interface ContractReceiptDao extends BaseDao<ContractReceiptEntity> {

    void insertBatch(List<ContractReceiptEntity> list);

    void insertBatchReceiptInfo(List<ContractReceiptInfoEntity> list);

    @Select("select SUM(i.money) from contract_receipt r LEFT JOIN contract_receipt_info i on r.id = i.contract_receipt_id where i.id is not null and  r.project_id = #{value}")
    BigDecimal receiptMoney(String projectId);

    //根据收款合同统计收款金额
    @Select("select sum(i.money) from contract_receipt_info i where i.contract_receipt_id = #{value}")
    BigDecimal countReceiptMoney(String receipt);

    @Select("select t.id from contract_payment t where t.project_id = #{value}")
    List<String> getPayList (String projectId);

    @Select("select t.id from contract_payment t where t.contract_receipt_id = #{value}")
    List<String> getpaymentIdList (String projectId);

    BigDecimal paymentMoney(List<String> list);

    @Select("select r.*,t.contract_type from contract_receipt r LEFT JOIN contract_type t ON r.contract_type_id = t.id where r.project_id = #{value}")
    List<ContractReceiptEntity> getContractReceiptOnLimt(String str);

    @Select("SELECT COUNT(*) FROM contract_receipt t LEFT JOIN contract_type c ON t.contract_type_id = c.id where t.project_id = #{value}")
    int getContractReceiptCount(String str);

    //根据收款合同ID统计付款总额
    @Select("select sum(i.payment_money) from payment_info i where i.contract_payment_id =  #{value}")
    BigDecimal sumPaymentMoney(String receiptId);

    @Delete("delete from contract_receipt where id = #{value}")
    void deleteContractReceipt(String id);

    void deleteBatchContractReceiptId(List<String> list);

    void deleteBatchContractInfoId(List<String> list);

    @Delete("delete from contract_payment where id = #{value}")
    void deleteContractPayment(String id);

    @Delete("delete from payment_info where contract_payment_id = #{value}")
    void deleteContractPaymentInfo(String id);

    @Delete("delete from payment_info where id = #{value}")
    void deletePaymentProportionInfo(String id);

    List<ContractReceiptExportEntity> getContractReceiptAll(String id);

    List<ContractReceiptEntity> getContractReceipt(Map<String,Object> params);

    List<ContractPaymentEntity> getContractPatment(List<String> list);

    List<PaymentInfoEntity> getPaymentInfo(List<String> list);

    @Select("SELECT" +
            " COUNT(*) " +
            " FROM" +
            " contract_receipt r" +
            " JOIN contract_payment p ON r.id = p.contract_receipt_id" +
            " JOIN payment_info i ON p.id = i.contract_payment_id " +
            " WHERE" +
            " r.id = #{value}")
    int sumPaymentInfo(String str);

    @Select("select sum(contract_total) from contract_receipt where tender_batch = #{value}")
    BigDecimal sumContractTotal(String tenderBatch);
	
}
